#include "sdl.h"

#include "globals.hpp"

#define KEY_NONE 0
#define KEY_DOWN 1
#define KEY_UP 2

char key[323] = {0};
char mouse_key[5] = {0};
int mouse_x;
int mouse_y;
bool mouse_motion = false;

//=============================================================================
//stav klavesnice a mysi
void stav_vstupu()
{
    SDL_Event event;
    //KONTROLA STAVU KLAVES
    for(int i=0;i<324;i++)
    {
        if(key[i] == KEY_UP)
            {
                key[i] = KEY_NONE;
            }
    }
    for(int i=0;i<6;i++)
    {
        if(mouse_key[i] == KEY_UP)
            {
                mouse_key[i] = KEY_NONE;
            }
    }
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)      //kdyby nebyl fullscreen, muzeme kliknutim na krizek ukoncit
        {
            done = true;
        }
        if(event.type == SDL_KEYDOWN)
        {
            key[event.key.keysym.sym] = KEY_DOWN;
        }
        else if(event.type == SDL_KEYUP)
        {
            key[event.key.keysym.sym] = KEY_UP;
        }
        if(event.type == SDL_MOUSEBUTTONDOWN)
        {
            mouse_key[event.button.button] = KEY_DOWN;
        }
        else if(event.type == SDL_MOUSEBUTTONUP)
        {
            mouse_key[event.button.button] = KEY_UP;
        }
        if(event.type == SDL_MOUSEMOTION)
        {
            mouse_x = event.motion.x;
            mouse_y = event.motion.y;
            mouse_motion = true;
        } else mouse_motion = false;

    }
}

//=============================================================================
//klavesnice
void klavesnice()
{
    if(key[SDLK_ESCAPE] == KEY_UP)  //(az kdyz) pustime "Esc"
        {done = true;}              //a ukoncime program

}

//=============================================================================
//mys
void mys()
{
    if ((mouse_x <=1) && (mouse_y <= 1)) done = true;   //kdyz najedeme kursorem do leveho horniho rohu, tak ukoncime program
}
